package com.manage.activity;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.manage.utils.AppConstans;
import com.manage.utils.AppUtils;

public class PasswordSettingActivity extends Activity {

	private String userPassword, oldPassStr, newPassStr, passRepeatStr;
	private Button btnCommit, btnReturn, btnResetPassword;
	private TextView oldPass, pass, passRepeat;
	private EditText oldPassTxt, newPassTxt, passRepeatTxt;
	private Bundle extras;
	private PasswordSettingActivity thiz;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_password);
		thiz = this;
		initPassword();

		extras = this.getIntent().getExtras();
		btnReturn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				BugMainActivity.switchActivity(SettingActivity.class);
				// returnToLastPage(extras == null ? null :
				// extras.getString(AppConstans.LAST_PAGE_FLAG));
			}
		});
		checkOldPwdShownNeeded();
		btnCommit.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				oldPassStr = oldPassTxt.getText().toString().trim();
				newPassStr = newPassTxt.getText().toString().trim();
				passRepeatStr = passRepeatTxt.getText().toString().trim();
				if (userPassword != null && !userPassword.trim().equals("")
						&& !AppUtils.getMD5(oldPassStr).equals(userPassword)) {
					showToast(getResources().getString(R.string.old_passwrod_error));
					return;
				}
				if (!AppUtils.isRegular(newPassStr, AppConstans.PASSWORD_FORMAT)) {
					showToast(getResources().getString(R.string.password_format_error));
					return;
				}
				if (!passRepeatStr.equals(newPassStr)) {
					showToast(getResources().getString(R.string.password_different));
					return;
				}

				if (extras == null) {
					// returnToLastPage(CalculatePeriodActivity.class.toString());
					return;
				}

			}
		});
		btnResetPassword.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showResetPwdDialog();
			}
		});
	}

	private void checkOldPwdShownNeeded() {
		if (userPassword == null || userPassword.trim().equals("")) {
			this.oldPass.setVisibility(View.GONE);
			this.oldPassTxt.setVisibility(View.GONE);
			this.pass.setText(R.string.password_txt);
			this.passRepeat.setText(R.string.repeat_password_txt);
			this.btnResetPassword.setVisibility(View.GONE);
		}
	}

	private void showResetPwdDialog() {
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(thiz);
		dialogBuilder.setMessage(thiz.getResources().getString(R.string.password_confirm));
		dialogBuilder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {

				Intent intent = AppConstans.intents.periodNotice(thiz);
				PendingIntent sender = PendingIntent.getBroadcast(thiz, 0, intent, 0);
				AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
				alarm.cancel(sender);
				checkOldPwdShownNeeded();
			}
		});
		dialogBuilder.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				dialog.dismiss();
			}
		});
		dialogBuilder.show();
	}

	private void initPassword() {

		oldPass = (TextView) findViewById(R.id.old_pass);
		oldPassTxt = (EditText) findViewById(R.id.old_pass_txt);
		newPassTxt = (EditText) findViewById(R.id.pass_txt);
		passRepeatTxt = (EditText) findViewById(R.id.pass_repeat_txt);
		btnCommit = (Button) findViewById(R.id.password_submit);
		btnReturn = (Button) findViewById(R.id.password_return);
		btnResetPassword = (Button) findViewById(R.id.btn_reset_password);
		pass = (TextView) findViewById(R.id.pass);
		passRepeat = (TextView) findViewById(R.id.pass_repeat);
		oldPass = (TextView) findViewById(R.id.old_pass);
	}

	private void showToast(String string) {
		Toast toast = Toast.makeText(PasswordSettingActivity.this, string, Toast.LENGTH_SHORT);
		toast.show();
	}

}
